package cn.tedu.controller;


import cn.tedu.pojo.User;
import cn.tedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
    public class UserController {

        @Autowired
        private UserService userService;

        @GetMapping("/login")
        public String getLogin(Model model) {
            return "Login";
        }

        @PostMapping("/register")
        public String getRegister(Model model){
            return "Register";
        }

        @PostMapping("/registerUser")
        public String registerGetLogin(@RequestParam("name") String name, @RequestParam("pwd") String password, Model model){
            User user = new User();
            user.setUserName(name);
            user.setUserPassword(password);
            user.setCreateTime(new Date());
            int res = userService.insert(user);
            return "Login";
        }

        @PostMapping("/login")
        public String loginGetMain(@RequestParam("name") String name,@RequestParam("pwd") String password, Model model){
            User user = new User();
            user.setUserName(name);
            user.setUserPassword(password);
            long res = userService.selectUser(user);
            if(res>0){
                //登录成功
                model.addAttribute("message","登录成功！");
                return "Login";
            }else{
                model.addAttribute("message","用户名或密码错误，请重试！");
                return "Login";
            }
        }

    }

